package org.raphets.todayinhistory.ui.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import org.raphets.todayinhistory.R;
import org.raphets.todayinhistory.utils.BaseDialog;
import org.raphets.todayinhistory.utils.WeakHandler;

import butterknife.ButterKnife;

public class WelcomeActivity extends AppCompatActivity {
    private boolean go2Setting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        if (!isNetWorkConnected(this)) {
            Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
            netSetting();
        }else {
            go2Main();
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (go2Setting) {
            if (netSettingDialog.isShowing()) {
                netSettingDialog.dismiss();
            }
            if (isNetWorkConnected(this)) {
                go2Main();
            }
        }
    }

    private BaseDialog netSettingDialog;

    private void netSetting()
    {
        netSettingDialog = new BaseDialog(this);
        netSettingDialog.setTitle("提示");
        netSettingDialog.setMessage("当前网络不可用，请先设置网络！");
        netSettingDialog.setButton1("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (i == 0)
                {
                    if (netSettingDialog.isShowing()){
                        netSettingDialog.dismiss();
                    }
                    Intent intent = null;
                    /**
                     * 判断手机系统的版本！如果API大于10 就是3.0+
                     * 因为3.0以上的版本的设置和3.0以下的设置不一样，调用的方法不同
                     */
                    if (android.os.Build.VERSION.SDK_INT > 10) {
                        intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                    } else {
                        intent = new Intent();
                        ComponentName component = new ComponentName(
                                "com.android.settings",
                                "com.android.settings.WirelessSettings");
                        intent.setComponent(component);
                        intent.setAction("android.intent.action.VIEW");
                    }
                    go2Setting = true;
                    startActivity(intent);
                }
            }
        });
        netSettingDialog.setButton2("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (i == 1){
                    if (netSettingDialog.isShowing()){
                        netSettingDialog.dismiss();
                    }
                    finish();
                }
            }
        });
        netSettingDialog.show();
    }


    private void go2Main(){
        new WeakHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
                finish();
            }
        },1500);
    }

    /**
     * check if network avalable
     *
     * @param context
     * @return
     */
    private boolean isNetWorkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable() && mNetworkInfo.isConnected();
            }
        }

        return false;
    }
}


